package com.happts.Service;

import com.happts.Dao.RECORDDao;
import com.happts.Entity.RECORD;

import java.util.List;

public class RECORDService {
    private RECORDDao Dao = new RECORDDao();

    public boolean add(RECORD record){
        return Dao.insert(record);
    }

    public boolean delete(String CITE,String NUMBER){
        return Dao.delete(CITE,NUMBER);
    }

    public boolean update(RECORD record){
        return Dao.update(record);
    }

    public List<RECORD> findAll(){
        return Dao.findALL();
    }

    public List<RECORD> find(String columnLable,String value){
        return Dao.find(columnLable,value);
    }

    public List<RECORD> findOne(String CITE,String NUMBER){
        return Dao.findOne(CITE,NUMBER);
    }

    public FetchOilResult fetchOil(String CITE,String NUMBER,Boolean isVeg,double WEIGHT){
        //WEIGHT是取油量，不是取菜籽的量
        List<RECORD> list = findOne(CITE,NUMBER);
        if (list.size()!=1) {
            return FetchOilResult.RECORD_NOTFOUND;
        }else {
            RECORD one = list.get(0);
            double rate;
            if(isVeg){
                double left = one.getLeftVeg();
                if(left<WEIGHT){
                    return FetchOilResult.LEFT_NOTENOUGH;
                }else {
                    one.setLeftVeg(left-WEIGHT);
                    rate = one.getVegRate();
                    one.setWeight((int)(one.getWeight()-WEIGHT/rate));
                    one.setLeftRefined(one.getWeight()*one.getRefinedRate());
                }
            }else {
                double left = one.getLeftRefined();
                if(left<WEIGHT){
                    return FetchOilResult.LEFT_NOTENOUGH;
                }else {
                    one.setLeftRefined(left-WEIGHT);
                    rate = one.getRefinedRate();
                    one.setWeight((int)(one.getWeight()-WEIGHT/rate));
                    one.setLeftVeg(one.getWeight()*one.getVegRate());
                }
            }
//            one.setWeight((int)(one.getWeight()-WEIGHT/rate));
            Dao.update(one);
            return FetchOilResult.SUCCESS;
        }
    }

    private double Veg_Refined_transfer(RECORD record, Boolean isVegWeight, double weight){
        double vegRate = record.getVegRate();
        double refinedRate = record.getRefinedRate();

        if (isVegWeight){
            //返回refinedLeft
            return (weight/vegRate)*refinedRate;
        }else {
            return (weight/refinedRate)*vegRate;
        }
    }
}